package br.com.validator.edital;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value="valorValidator")
public class valorValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		
		Pattern p = Pattern.compile("^(\\d{1,3}[ ,]?)*(\\.\\d{0,2})?$");
		Matcher m = p.matcher(String.valueOf(value));
		if(!m.matches()){
			((UIInput)component).setValid(false);
				FacesMessage message = new FacesMessage();
					message.setDetail("Ex: 11.00");
					
					message.setSeverity(FacesMessage.SEVERITY_ERROR);
				context.addMessage("valor", message);
				
				throw new ValidatorException(message);
					
		}
		
	}

}
